package com.cheejee.goodscenter.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cheejee.goodscenter.dao.SkuMapper;
import com.cheejee.goodscenter.dao.SkuSnapshotMapper;
import com.cheejee.goodscenter.module.dto.SkuDto;
import com.cheejee.goodscenter.module.dto.SkuSnapshotDto;
import com.cheejee.goodscenter.module.dto.SkuSpecificDto;
import com.cheejee.goodscenter.module.entity.SkuSnapshot;
import com.cheejee.goodscenter.service.SkuService;
import com.cheejee.goodscenter.service.SkuSnapshotService;
import com.cheejee.goodscenter.service.exception.DataNotExistException;
import com.cheejee.goodscenter.validated.Check;
import com.cheejee.goodscenter.validated.InsertGroup;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author CARRY
 * @Date 2020年11月3日上午1:21:14
 */
@Slf4j
@Service
public class SkuSnapshotServcieImpl implements SkuSnapshotService {

	@Resource
	private SkuSnapshotMapper mapper;

	@Resource
	private SkuService skuService;

	@Resource
	private SkuMapper skuMapper;
	
	@Resource
	private ObjectMapper json;

	@Override
	public PageInfo<SkuSnapshotDto> querySkuSnapshot(Long goodsId, Long skuId, Integer version, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);

		return new PageInfo<>(convert(mapper.selectBySelective(skuId, goodsId, version)));

	}

	@Override
	public SkuSnapshotDto querySkuSnapshotById(Long id) {
		SkuSnapshot s = mapper.selectByPrimaryKey(id);

		return s == null ? null : convert(Arrays.asList(s)).get(0);
	}

	@Override
	@Check(message = "新增的SKU快照不能为空", group = InsertGroup.class)
	public int addSkuSnapshot(SkuSnapshotDto skuSnapshot) {
		SkuSnapshot s = convertDto(skuSnapshot);
		int i = mapper.insertSelective(s);
		skuSnapshot.setId(s.getId());

		return i;
	}

	@Override
	public Long addSkuSnapshot(Long skuId) {
		SkuDto d = skuService.querySkuInfoById(skuId);

		if (d == null) {
			throw new DataNotExistException("SKU不存在");
		}

		SkuSnapshotDto s = BeanUtil.copyProperties(d, SkuSnapshotDto.class, "id", "specific");
		s.setSkuId(d.getId());
		s.setSpecificGroup(d.getSpecific());
		SkuSnapshot ss = convertDto(s);
		ss.setVersion(skuMapper.selectVersion(skuId));
		int i = mapper.insertSelective(ss);

		return i == 1 ? ss.getId() : 0L;
	}

	@Override
	public int deleteSkuSnapshot(Long id) {

		return mapper.deleteByPrimaryKey(id);
	}

	private List<SkuSnapshotDto> convert(List<SkuSnapshot> ss) {
		List<SkuSnapshotDto> re = new ArrayList<>();
		
		if (CollectionUtil.isEmpty(ss)) {
			return re;
		}
		
		for (SkuSnapshot i : ss) {
			SkuSnapshotDto s = BeanUtil.copyProperties(i, SkuSnapshotDto.class, "specificGroup");
			List<SkuSpecificDto> ld = null;
			
			try {
				String str = i.getSpecificGroup();
				System.out.println(JSONUtil.isJsonArray(str));
				ld = JSONUtil.parseArray(str).toList(SkuSpecificDto.class);
				
			} catch (Exception e) {
				log.error("解析JSON字串失败: {}", i.getSpecificGroup());
			}
			
			s.setSpecificGroup(ld);
			re.add(s);
		}

		return re;
	}

	public static SkuSnapshot convertDto(SkuSnapshotDto ss) {
		if (ss == null) {
			return null;
		}

		SkuSnapshot s = BeanUtil.copyProperties(ss, SkuSnapshot.class, "specificGroup");
		s.setSpecificGroup(JSONUtil.parseArray(ss.getSpecificGroup()).toJSONString(2));

		return s;
	}

}
